<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_Model extends CI_Model{
	protected $_name = "users";
	protected $_primary = "UserId";
	var $UserName = "UserName", $FullName = "FullName", $Email = "Email", $Password = "Password", $Status = "Status";
	
	function checkLogin(){
		$username = $this->security->xss_clean($this->input->post('UserName'));
		$password = $this->security->xss_clean($this->input->post('Password'));
		$this->db->where($this->UserName, $username);
		$this->db->where($this->Password, md5(md5($password)));
		$this->db->where($this->Status, 1);
		
		$query = $this->db->get($this->_name);
		if($query->num_rows == 1){
			$row = $query->row();
			$data = array(
				"UserId" => $row->UserId,
				"FullName" => $row->FullName,
				"UserName" => $row->UserName,
				"state_login_admin" => true,
				"p" => md5(md5("ADMIN"))
			);
			$this->session->set_userdata($data);
			return true;
		}
		return false;
	}
	
	function checkExist($field){
		$value = trim($this->input->post($field, true));
		$this->db->where($field, $value);
		$query = $this->db->get($this->_name);
		if($query->num_rows == 0){
			return true;
		}
		return false;
	}
	
	
	function insertUser($password){
		try{
			$this->db->set($this->UserName, $this->input->post('UserName', true));
			$this->db->set($this->FullName, $this->input->post('FullName', true));
			$this->db->set($this->Email, $this->input->post('Email', true));
			$this->db->set($this->Password, md5(md5($password)));
			$this->db->set($this->Status, 1);
			$this->db->insert($this->_name);
			return true;
		}catch(Exception $e){
			return false;
		}
	}
	
	function updateUser($UserId){
		$data = array(
					$this->FullName => trim($this->input->post($this->FullName, true)),
					$this->Email => trim($this->input->post($this->Email, true))
				);
		$this->db->where($this->_primary, $UserId);
		$this->db->update($this->_name, $data);
	}
	
	function countUsers(){
		$this->db->where($this->Status, 1);
		$this->db->or_where($this->Status, 2);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function getUsersLimit($currentPage, $number){
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$this->db->limit($number, $currentPage);
		$this->db->order_by($this->_primary, 'DESC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function searchUser($keysearch, $fieldsearch){
		switch($fieldsearch){
			case 1:
				$this->db->like($this->UserName, $keysearch);	
				break;
			case 2:
				$this->db->like($this->FullName, $keysearch);	
				break;
			default:
				$this->db->like($this->UserName, $keysearch);	
				break;
		}
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->_primary, 'DESC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function changeStatusUser($UserId, $Status){
		try{
			$this->db->where($this->_primary, $UserId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function deleteUser($UserId){
		try{
			$this->db->where($this->_primary, $UserId);
			$this->db->update($this->_name, array($this->Status => 3));
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function getUserByUserId($UserId){
		$this->db->where($this->_primary, $UserId);
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
}